package baseFx.common.utils;

import java.io.*;

public class StreamUtils {
    public static InputStream convert(ByteArrayOutputStream stream) {
        return new ByteArrayInputStream(stream.toByteArray());
    }

    public static ByteArrayOutputStream convert(InputStream stream) {
        try {
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] data = new byte[4096];
            int ch;
            while ((ch = stream.read(data, 0, data.length)) > 0) {
                swapStream.write(data, 0, ch);
            }
            return swapStream;
        } catch (Exception e) {
            throw ExceptionUtils.toRuntime(e);
        }
    }

    public static void write(InputStream inputStream, OutputStream outputStream) {
        try {
            byte[] data = new byte[4096];
            int ch;
            while ((ch = inputStream.read(data, 0, data.length)) > 0) {
                outputStream.write(data, 0, ch);
            }
        } catch (IOException e) {
            throw ExceptionUtils.toRuntime(e);
        }
    }


}
